package com.yenlien.traceyun.plant.controller;

import com.yenlien.traceyun.plant.entity.Process;
import com.yenlien.traceyun.plant.service.ProcessService;
import com.yenlien.traceyun.webase.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 加工
 *
 * @author maybe
 * @date 2020年7月28日
 */
@RestController
@RequestMapping("/process")
public class ProcessController extends BaseController {
    @Autowired
    private ProcessService processService;

    //新增
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(Process process, HttpServletRequest request) throws Exception {
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("files"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("files");
        }
        processService.save(process, files);
        return success("success");
    }


    @RequestMapping(value = "/save2", method = RequestMethod.POST)
    public Object save2(@RequestBody Process process) throws Exception {
        processService.save2(process);
        return success("success");
    }

    //根据ID查询详情
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public Object findByProcessId(@PathVariable("id") String id) {
        return success(processService.findById(id));
    }
}
